<?php

class UsineCommande {
	public static function creerInstance($config, $debug=false) {
		if ( !file_exists("$config") ) {
			echo "\nWarn : config $config manquant ";
			return null;
		}
		$xml = simplexml_load_file($config);
 
		
		$tabClasse=array();
		foreach ($xml->Commande as $cmd) {
			$attrs=$cmd->attributes();
			$id=$attrs['id'];
			if ($debug) echo "\n* Creation de la commande: ".$cmd->nom. " ($id)";
			$class_name=$cmd->classe->__toString();
			$objCmd= new $class_name();
			$tabClasse["$id"]=$objCmd;
		}
		krsort($tabClasse);
		if ($debug) print_r($tabClasse);
		$prev=NULL;
		$last=NULL;
		foreach ( $tabClasse as $id => $cmd) {
			if ($debug) echo "\n$id) ".print_r($cmd, true);
			if ($last !=NULL) $cmd->positionnerSuivante($last);
			$last=$cmd;
		}
		return $last;
	}
}
?>